
package Frontera;

import DAO.ClienteDAO;
import DAO.MuestraDAO;
import Entidad.Cliente;
import Entidad.Muestra;
import control.ConexionDB;
import control.ControlCliente;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class FBuscarMuestras extends javax.swing.JFrame {

    public FBuscarMuestras() {
        initComponents();    
        this.setLocationRelativeTo(null);
        cargarClientes();
        tca = new TableColumnAdjuster(Tresultados);
        tca.adjustColumns();
        SNumeroSolicitud.setValue(1);
    }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        CBSolicitud = new javax.swing.JCheckBox();
        CBIdentificacion = new javax.swing.JCheckBox();
        CkBCliente = new javax.swing.JCheckBox();
        CBCantidad = new javax.swing.JCheckBox();
        CBRadioactiva = new javax.swing.JCheckBox();
        CBMagnetica = new javax.swing.JCheckBox();
        TFIdentificacion = new javax.swing.JTextField();
        BBuscar = new javax.swing.JButton();
        BCancelar = new javax.swing.JButton();
        SNumeroSolicitud = new javax.swing.JSpinner();
        CBClientes = new javax.swing.JComboBox();
        CBConsultarTodas = new javax.swing.JCheckBox();
        LEntre = new javax.swing.JLabel();
        SCantidadMin = new javax.swing.JSpinner();
        LY = new javax.swing.JLabel();
        SCantidadMax = new javax.swing.JSpinner();
        LGramos = new javax.swing.JLabel();
        CkBDisposicion = new javax.swing.JCheckBox();
        CBDisposicion = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tresultados = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        LResultadosEncontrados = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Muestras");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        CBSolicitud.setText("Solicitud:");
        CBSolicitud.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBSolicitudActionPerformed(evt);
            }
        });

        CBIdentificacion.setText("Identificación:");
        CBIdentificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBIdentificacionActionPerformed(evt);
            }
        });

        CkBCliente.setText("Cliente:");
        CkBCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CkBClienteActionPerformed(evt);
            }
        });

        CBCantidad.setText("Cantidad:");
        CBCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBCantidadActionPerformed(evt);
            }
        });

        CBRadioactiva.setText("Radioactiva");

        CBMagnetica.setText("Magnética");

        TFIdentificacion.setEnabled(false);

        BBuscar.setText("Buscar");
        BBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BBuscarActionPerformed(evt);
            }
        });

        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        SNumeroSolicitud.setEnabled(false);

        CBClientes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cargando lista de clientes..." }));
        CBClientes.setEnabled(false);

        CBConsultarTodas.setText("Consultar Todas");
        CBConsultarTodas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBConsultarTodasActionPerformed(evt);
            }
        });

        LEntre.setText("Entre");

        SCantidadMin.setEnabled(false);

        LY.setText("y");

        SCantidadMax.setEnabled(false);

        LGramos.setText("gramos.");

        CkBDisposicion.setText("Disposición:");
        CkBDisposicion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CkBDisposicionActionPerformed(evt);
            }
        });

        CBDisposicion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Entregar", "Disponer" }));
        CBDisposicion.setEnabled(false);

        Tresultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Solicitud", "Código Alpha", "Identificación", "Nombre del Cliente", "Radioactiva", "Magnética", "Cantidad", "Observaciones"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        Tresultados.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(Tresultados);

        jLabel1.setText("Filtrar la Búsqueda  Por: ");

        LResultadosEncontrados.setText("    ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(CkBCliente)
                                        .addGap(18, 18, 18)
                                        .addComponent(CBClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(CBSolicitud)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(SNumeroSolicitud, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(64, 64, 64)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(CBIdentificacion)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(TFIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                .addComponent(CkBDisposicion)
                                                .addGap(18, 18, 18)
                                                .addComponent(CBDisposicion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                                .addComponent(CBRadioactiva)
                                                .addGap(18, 18, 18)
                                                .addComponent(CBMagnetica)))
                                        .addGap(18, 18, 18)
                                        .addComponent(CBConsultarTodas)))
                                .addGap(0, 46, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(BBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(BCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(CBCantidad)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(LEntre)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SCantidadMin, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(LY)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SCantidadMax, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(LGramos)
                                    .addComponent(LResultadosEncontrados))))
                        .addGap(0, 390, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBSolicitud)
                    .addComponent(SNumeroSolicitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBIdentificacion)
                    .addComponent(TFIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CkBCliente)
                    .addComponent(CBClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CkBDisposicion)
                    .addComponent(CBDisposicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBCantidad)
                    .addComponent(LEntre)
                    .addComponent(SCantidadMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LY)
                    .addComponent(SCantidadMax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LGramos)
                    .addComponent(CBRadioactiva)
                    .addComponent(CBMagnetica)
                    .addComponent(CBConsultarTodas))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BBuscar)
                            .addComponent(BCancelar))
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(LResultadosEncontrados)
                        .addGap(3, 3, 3)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BBuscarActionPerformed
       ConexionDB conexionBaseDeDatos = new ConexionDB();
       Connection conexion = conexionBaseDeDatos.establecerConexion();
        
       ArrayList <String> criterios = new ArrayList();
       ArrayList <Object> valores = new ArrayList();
       muestraDAO = new MuestraDAO();
       String abreviaturaCliente=null;
       int nCriterios = 0;
       
       /*Extraccion de los nombres de los criterios de busqueda y los valores que se pretenden buscar*/
       if(CBSolicitud.isSelected()){
           criterios.add("solicitudes_consecutivo_de_solicitud"); 
           valores.add(SNumeroSolicitud.getValue());
           nCriterios++;    
       }
       if(CkBCliente.isSelected()){
           /*cargar el cliente para optener la abreviatura, y con ella optener la muestra*/
            try {
                clienteBuscado = clienteDAO.eoncontrarClientePorNombre(CBClientes.getSelectedItem().toString(), conexion);
                abreviaturaCliente = clienteBuscado.getAbreviatura();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
           criterios.add("clientes_abreviatura"); 
           valores.add(abreviaturaCliente);
           nCriterios++;
       }
       if(CBIdentificacion.isSelected()){
           criterios.add("identificacion"); 
           valores.add(TFIdentificacion.getText().trim());
           nCriterios++;
       } 
       if(CBIdentificacion.isSelected()){
           criterios.add("identificacion"); 
           valores.add(TFIdentificacion.getText().trim());
           nCriterios++;
       }
       if(CkBDisposicion.isSelected()){
           criterios.add("tipo_disposicion"); 
           valores.add(CBDisposicion.getSelectedIndex());
           nCriterios++;
       }
       if(CBCantidad.isSelected()){
           criterios.add("cantidad"+" >"); 
           criterios.add("cantidad"+" <");        
           valores.add(SCantidadMin.getValue());
           valores.add(SCantidadMax.getValue());
           nCriterios = nCriterios + 2;
       }
       if(CBRadioactiva.isSelected()){
           criterios.add("radioactiva"); 
           valores.add(CBRadioactiva.isSelected());
           nCriterios++;
       }
       if(CBMagnetica.isSelected()){
           criterios.add("magnetica"); 
           valores.add(CBMagnetica.isSelected());
           nCriterios++;
       }
       if(CBConsultarTodas.isSelected()){
           nCriterios=-1;
       }
       
       try {
             switch(nCriterios){  
                   case -1: setResultOnTable(muestraDAO.loadAll(conexion),conexion); break;          
                   case 0: JOptionPane.showMessageDialog(null, "No se ha seleccionado ningún criterio de búsqueda.");break;
                   case 1: setResultOnTable(muestraDAO.BusquedaUnCriterio(criterios, valores, conexion),conexion); break;                 
                   case 2: setResultOnTable(muestraDAO.BusquedaDosCriterios(criterios, valores, conexion),conexion); break;
                   case 3: setResultOnTable(muestraDAO.BusquedaTresCriterios(criterios, valores, conexion),conexion); break; 
                   case 4: setResultOnTable(muestraDAO.BusquedaCuatroCriterios(criterios, valores, conexion),conexion); break;      
                   case 5: setResultOnTable(muestraDAO.BusquedaCincoCriterios(criterios, valores, conexion),conexion); break;     
                   case 6: setResultOnTable(muestraDAO.BusquedaSeisCriterios(criterios, valores, conexion),conexion); break;        
                   case 7: setResultOnTable(muestraDAO.BusquedaSieteCriterios(criterios, valores, conexion),conexion); break;        
                   case 8: setResultOnTable(muestraDAO.BusquedaOchoCriterios(criterios, valores, conexion),conexion); break;     
             }
       } catch (SQLException e){
           e.printStackTrace();
       } catch (Exception e){
           e.printStackTrace();
       }
       
    }//GEN-LAST:event_BBuscarActionPerformed

    private void CBSolicitudActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBSolicitudActionPerformed
        if(CBSolicitud.isSelected())
        SNumeroSolicitud.setEnabled(true);
        else
        SNumeroSolicitud.setEnabled(false);       
    }//GEN-LAST:event_CBSolicitudActionPerformed

    private void CkBClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CkBClienteActionPerformed
        if(CkBCliente.isSelected())
        CBClientes.setEnabled(true);
        else
        CBClientes.setEnabled(false);  
    }//GEN-LAST:event_CkBClienteActionPerformed

    private void CBIdentificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBIdentificacionActionPerformed
        if (CBIdentificacion.isSelected()) 
            TFIdentificacion.setEnabled(true);
        else 
            TFIdentificacion.setEnabled(false);
        
    }//GEN-LAST:event_CBIdentificacionActionPerformed

    private void CBCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBCantidadActionPerformed
        if (CBCantidad.isSelected()) {
            SCantidadMin.setEnabled(true);
            SCantidadMax.setEnabled(true);
        } else {
            SCantidadMin.setEnabled(false);
            SCantidadMax.setEnabled(false);
        }
    }//GEN-LAST:event_CBCantidadActionPerformed

    private void CkBDisposicionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CkBDisposicionActionPerformed
        if (CkBDisposicion.isSelected()) {
            CBDisposicion.setEnabled(true);
        } else {
            CBDisposicion.setEnabled(false);
        }
    }//GEN-LAST:event_CkBDisposicionActionPerformed

    private void CBConsultarTodasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBConsultarTodasActionPerformed
        if(CBConsultarTodas.isSelected()){ 
            CBSolicitud.setEnabled(false);
            CBSolicitud.setSelected(false);
            CkBCliente.setEnabled(false);
            CkBCliente.setSelected(false);
            CBCantidad.setEnabled(false);
            CBCantidad.setSelected(false);
            SCantidadMin.setEnabled(false);
            SCantidadMax.setEnabled(false);
            TFIdentificacion.setEnabled(false);
            CBClientes.setEnabled(false);  
            SNumeroSolicitud.setEnabled(false); 
            CBIdentificacion.setEnabled(false);
            CBIdentificacion.setSelected(false);
            CkBDisposicion.setEnabled(false);
            CkBDisposicion.setSelected(false);
            CBRadioactiva.setEnabled(false);    
            CBRadioactiva.setSelected(false);     
            CBMagnetica.setEnabled(false);  
            CBMagnetica.setSelected(false);  
            CBDisposicion.setEnabled(false);
            LEntre.setEnabled(false);
            LY.setEnabled(false);
            LGramos.setEnabled(false);
        }else {   
            CBCantidad.setEnabled(true);
            CkBCliente.setEnabled(true);
            CkBDisposicion.setEnabled(true);
            CBRadioactiva.setEnabled(true);
            CBSolicitud.setEnabled(true);
            CBMagnetica.setEnabled(true);
            CBIdentificacion.setEnabled(true);    
            LEntre.setEnabled(true);
            LY.setEnabled(true);
            LGramos.setEnabled(true);
        }    
    }//GEN-LAST:event_CBConsultarTodasActionPerformed

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
         this.dispose();
    }//GEN-LAST:event_BCancelarActionPerformed
 
    public void cargarClientes(){
        CBClientes.setEnabled(false);
        CBClientes.setModel(new DefaultComboBoxModel(new Object[]{"Cargando lista de clientes..."}));
        TrabajadorCargarClientes cargarClientes =
                new TrabajadorCargarClientes(controlCliente){
            @Override
            protected void done(){
                try {
                    nombresClientes = get();
                    CBClientes.setModel(new DefaultComboBoxModel(nombresClientes));
                    CBClientes.setSelectedIndex(0);
                } catch (InterruptedException | ExecutionException e){
                    JOptionPane.showMessageDialog(FBuscarMuestras.this, e.getMessage(),
                    "Error en hilo trabajador", javax.swing.JOptionPane.ERROR_MESSAGE);
                }
            }
        };  
        cargarClientes.execute();
    }
    
   private void setResultOnTable(List rs, Connection cnn) throws SQLException{
        resultados = (ArrayList<Muestra>)rs;
        DefaultTableModel modelo =(DefaultTableModel)Tresultados.getModel(); 
        cleanModel(modelo);
        
        if(resultados != null){
              
              for(int i=0; i<resultados.size(); i++){
                  clienteBuscado = optenerCliente(resultados.get(i).getAbreviaturaCliente(), cnn);
                  modelo.addRow(new Object[]{String.format("SA%04d", resultados.get(i).getConsecutivoSolicitud()), 
                                               String.format("%s%05d", resultados.get(i).getAbreviaturaCliente(), resultados.get(i).getCodigoAlpha()),
                                               resultados.get(i).getIdentificacion(), clienteBuscado.getNombre(), 
                                               resultados.get(i).isRadioactiva(), resultados.get(i).isMagnetica(),
                                               resultados.get(i).getCantidad()+" g", resultados.get(i).getObservaciones()});
              }
                    
              tca.adjustColumns();
        }else{
               JOptionPane.showMessageDialog(this,"No se encontraron resultados.");
        }
        LResultadosEncontrados.setText(modelo.getRowCount()+" Muestra(s) encontradas.");
        muestraDAO.cerrarCnn(cnn);
    }
   
    public Cliente optenerCliente(String abreviatura, Connection cnn){    
        try {
              return clienteDAO.eoncontrarClientePorAbreviatura(abreviatura ,cnn);
            } catch (SQLException ex) {
                ex.printStackTrace();
                return null;
            }
    }
    
   private void cleanModel(DefaultTableModel modelo){
        int nfilas = modelo.getRowCount();
        for(int i=nfilas-1; i>=0;i--){
        modelo.removeRow(i);
        } 
    }
        
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FBuscarMuestras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FBuscarMuestras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FBuscarMuestras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FBuscarMuestras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FBuscarMuestras().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BBuscar;
    private javax.swing.JButton BCancelar;
    private javax.swing.JCheckBox CBCantidad;
    private javax.swing.JComboBox CBClientes;
    private javax.swing.JCheckBox CBConsultarTodas;
    private javax.swing.JComboBox CBDisposicion;
    private javax.swing.JCheckBox CBIdentificacion;
    private javax.swing.JCheckBox CBMagnetica;
    private javax.swing.JCheckBox CBRadioactiva;
    private javax.swing.JCheckBox CBSolicitud;
    private javax.swing.JCheckBox CkBCliente;
    private javax.swing.JCheckBox CkBDisposicion;
    private javax.swing.JLabel LEntre;
    private javax.swing.JLabel LGramos;
    private javax.swing.JLabel LResultadosEncontrados;
    private javax.swing.JLabel LY;
    private javax.swing.JSpinner SCantidadMax;
    private javax.swing.JSpinner SCantidadMin;
    private javax.swing.JSpinner SNumeroSolicitud;
    private javax.swing.JTextField TFIdentificacion;
    private javax.swing.JTable Tresultados;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    private String[] nombresClientes;
    private ArrayList<Muestra> resultados;
    private MuestraDAO muestraDAO;
    private ClienteDAO clienteDAO = new ClienteDAO(); 
    private Cliente clienteBuscado;
    private ControlCliente controlCliente = new ControlCliente();
    private TableColumnAdjuster tca;
}
